using System;
using System.Data;
using System.Text;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Globalization;

using GonzalesCore.CoreElements;
using GonzalesCore.CoreManagers;
using Kypris2.CoreElements;
using Kypris2.Utils;
using Jamila2.Tools;
using System.IO;


public partial class GonzalesAdminActionLog : System.Web.UI.Page
{
    protected PrivateLoginSession CurrentPrivateSession = null;
    protected LogManager CurLogManager = null;

    #region CurrentPageIndex
    protected int CurrentPageIndex
    {
        get
        {
            return (int)ViewState["CurrentPageIndex"];
        }

        set
        {
            ViewState["CurrentPageIndex"] = value;
        }
    }
    #endregion

    #region ResultTable
    protected DataTable ResultTable
    {
        get
        {
            return (DataTable)ViewState["ResultTable"];
        }

        set
        {
            ViewState["ResultTable"] = value;
        }
    }
    #endregion

    #region GridViewHeader
    protected ArrayList GridViewHeader
    {
        get
        {
            return (ArrayList)ViewState["GridViewHeader"];
        }

        set
        {
            ViewState["GridViewHeader"] = value;
        }
    }
    #endregion

    #region Events
    protected void Page_Load(object sender, EventArgs e)
    {
        this.SessionCheck();

        this.Table_MessageBox.Visible = false;

        if (!IsPostBack)
        {
            // Thang Nguyen added 07/03/2013: default from date and to date ///////////////////////
            this.TextBox_DateFrom.Text = SwissArmy.DateConvert(DateTime.Today, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedDateFormat, "/", false).ToString();
            this.TextBox_DateTo.Text = SwissArmy.DateConvert(DateTime.Today, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedDateFormat, "/", false).ToString();
            ////////////////////////////////////////////////////////////////////////////////////////

            this.CurrentPageIndex = 0;
            this.PageComponentSetup();
            this.SwitchLanguage();

            this.LoadData();

            if (this.ResultTable != null)
            {
                DataView ResultView = new DataView(this.ResultTable);
                ResultView.Sort = this.DropDownList_SortBy.SelectedValue + " " + this.DropDownList_SortOrder.SelectedValue;
                this.BindData(ResultView, this.CurrentPageIndex);
                GridBuild("FullLog");
            }
            else
                this.ShowMessage("There's no data.");
        }
    }
    protected void DropDownList_SortBy_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (this.ResultTable != null)
        {
            DataView ResultView = new DataView(this.ResultTable);
            ResultView.Sort = this.DropDownList_SortBy.SelectedValue + " " + this.DropDownList_SortOrder.SelectedValue;
            this.BindData(ResultView, this.CurrentPageIndex);
            
        }
    }
    protected void DropDownList_SortOrder_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.DropDownList_SortBy_SelectedIndexChanged(sender, e);
    }
    protected void GridViewResult_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.CurrentPageIndex = e.NewPageIndex;
        this.DropDownList_SortBy_SelectedIndexChanged(sender, e);
    }
    protected void Button_Search_Click(object sender, EventArgs e)
    {
       try
       {
           this.LoadData();
            if (this.ResultTable != null)
            {
                DataView ResultView = new DataView(this.ResultTable);
                // Thang Nguyen comment 07032013
                /*ResultView.RowFilter = "";

                if (this.TextBox_User.Text.Length > 0 && this.TextBox_User.Text.CompareTo("[Personnel Name]") != 0)
                {
                    ResultView.RowFilter = "ID = '" + this.TextBox_User.Text + "'";
                }

                if (this.TextBox_DateFrom.Text.Length > 0 && this.TextBox_DateTo.Text.Length > 0)
                {
                    ResultView.RowFilter += (ResultView.RowFilter.Length > 0) ? " AND LogDateTime >= '" + SwissArmy.DateConvert(this.TextBox_DateFrom.Text, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedDateFormat).ToShortDateString() + "' AND LogDateTime <= '" + SwissArmy.DateConvert(this.TextBox_DateTo.Text, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedDateFormat).ToShortDateString() + "'" : " LogDateTime >= '" + SwissArmy.DateConvert(this.TextBox_DateFrom.Text, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedDateFormat).ToShortDateString() + "' AND LogDateTime <= '" + SwissArmy.DateConvert(this.TextBox_DateTo.Text, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedDateFormat).ToShortDateString() + "'";
                }*/
            
                this.CurrentPageIndex = 0;
                string str = ddlFiler.SelectedValue.ToString();
                GridBuild(str); // Thang Nguyen added 07032013
                //this.BindData(ResultView, this.CurrentPageIndex);
            }
       }
       catch (Exception ex)
       {
           this.ShowErrorMessage(ex.Message);
       }
    }

    protected void Button_ClearLog_Click(object sender, EventArgs e)
    {
        if (this.CurLogManager.ClearGeneralActionLog())
        {
            // log down the history
            this.CurLogManager.LogMessage("[GonzalesAdminActionLog] : Action Log Cleared", this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.User);

            this.CurrentPageIndex = 0;
            this.LoadData();
            DataView ResultView = new DataView(this.ResultTable);
            this.BindData(ResultView, this.CurrentPageIndex);
        }
    }

    #region OnRowCreated
    protected void OnRowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            TableCellCollection cells = e.Row.Cells;

            for (int i = 0; i < this.GridViewHeader.Count; i++)
            {
                cells[i].Text = this.GridViewHeader[i].ToString();
            }
        }
    }
    #endregion
    #endregion

    protected string GetLogTypeValue(string logtype)
    {
        switch (logtype)
        {
            case "FullLog":
                    return string.Empty;
            case "Exep":
                    return "''GonzalesAdminEASDetail''";
                
            case "ClaimantDetail":
                    return "''GonzalesAdminClaimantsDetail''";
                
            case "Authorizer":
                    return "''GonzalesAdminFIListing'',''GonzalesAdminAuthorizedListing'',''GonzalesAdminAuthorizedDetail''";
        }
        return string.Empty;
    }


    #region Customized Methods

    #region SessionCheck
    public void SessionCheck()
    {

        if (Session[GonzalesCommonNamingConvention.CurrentPrivateSession] != null)
        {
            this.CurrentPrivateSession = (PrivateLoginSession)Session[GonzalesCommonNamingConvention.CurrentPrivateSession];
            this.CurLogManager = new LogManager(this.CurrentPrivateSession);    // activate Log Manager
        }
        else
        {
            Response.Redirect(GonzalesErrorPageRedirect.SessionError);
        }

    }
    #endregion

    #region PageComponentSetup
    protected void PageComponentSetup()
    {

        this.TextBox_DateTo.Attributes["onclick"] = this.TextBox_DateFrom.Attributes["onclick"] = "popUpCalendar(this, this, '" + SwissArmy.DateFormatForDisplay(this.CurrentPrivateSession.DateFormatInUse, "/") + "');";

    }
    #endregion

    #region SwitchLanguage
    protected void SwitchLanguage()
    {
        try
        {
            using (MultiLingualFactory CurLanguageFactory = MultiLingualEngine.GetMultiLingualFactory(this, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedLanguage.InternalID))
            {
                this.Label_Information.Text = CurLanguageFactory.DisplayValue("Information");
                this.Label_PageTitle.Text = CurLanguageFactory.DisplayValue("PageTitle");
                this.Label_To.Text = CurLanguageFactory.DisplayValue("To");
                this.Button_ClearLog.Text = CurLanguageFactory.DisplayValue("ClearLog");
                this.btnExport.Text = CurLanguageFactory.DisplayValue("Output2Excel");
                this.Button_Search.Text = CurLanguageFactory.DisplayValue("Output2Screen");

                this.GridViewHeader = new ArrayList();
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("LogDateTime"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("Claimant"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("Description"));
                //FRANK Add JAN-13
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("log_interface"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("UserID"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("UserName"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("ClaimantID"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("ClaimantName"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("RoleID"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("Action"));

                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("ApproverID"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("ApproverName"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("DfltCurrency"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("CreditCardNo"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("AprStopPoint"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("AprLimit"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("NoAprRq"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("NoAuthorizerRq"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("AprRoute"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("AsClaimant"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("AsReviewer"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("LDAPAddress"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("AprRangeFr"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("AprRangeTo"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("LogType"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("AprLevel"));

                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("UserEmployeeID"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("UserEntity"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("ClaimantEmployeeID"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("ClaimantCompany"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("ApproverEmployeeID"));

                this.DropDownList_SortBy.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("LogDateTime"), "LogDateTime"));
                this.DropDownList_SortBy.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("Claimant"), "Claimant"));

                this.DropDownList_SortOrder.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("ASC"), "ASC"));
                this.DropDownList_SortOrder.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("DESC"), "DESC"));
                this.DropDownList_SortOrder.SelectedIndex = 1;
                //FRANK Add JAN-13
                this.ddlFiler.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("FullLog"), "FullLog"));
                this.ddlFiler.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("Exep"), "Exep"));
                this.ddlFiler.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("ClaimantDetail"), "ClaimantDetail"));
                this.ddlFiler.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("Authorizer"), "Authorizer"));

                this.Button_ClearLog.Attributes["onclick"] = "return confirm('"+ CurLanguageFactory.DisplayValue("ConfirmedMessage") +"');";

            }
        }
        catch (Exception ex)
        {
            this.CurLogManager.LogError("[GonzalesAdminAuthorizedDetail.SwitchLanguage.EX] : " + ex.Message, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.User);
        }

    }
    #endregion

    #region ShowErrorMessage
    protected void ShowErrorMessage(string ErrorMessage)
    {
        this.Table_MessageBox.Visible = true;
        this.Table_MessageBox.Rows[0].Cells[0].Text = ErrorMessage;
        this.Table_MessageBox.Rows[0].Cells[0].CssClass = "ErrorBox";
    }
    #endregion

    #region ShowMessage
    protected void ShowMessage(string Message)
    {
        this.Table_MessageBox.Visible = true;
        this.Table_MessageBox.Rows[0].Cells[0].Text = Message;
        this.Table_MessageBox.Rows[0].Cells[0].CssClass = "MessageBox";
    }
    #endregion

    #endregion

    #region Data Operation

    protected void LoadData()
    {
        // ThangNguyen Added 07/03/2013: Build condition ///////////////////////////////////////////////////////////////////////////
        string FromLogDateTime = "19000101", ToLogDateTime = "99991231";

        if (!this.TextBox_DateFrom.Text.Equals(""))
        {
            //string[] lsa_FromDate = this.TextBox_DateFrom.Text.Split('/');
            FromLogDateTime = SwissArmy.DateConvert(this.TextBox_DateFrom.Text, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedDateFormat).ToString("yyyyMMdd");
        }
        if (!this.TextBox_DateTo.Text.Equals(""))
        {
            //string[] lsa_ToDate = this.TextBox_DateTo.Text.Split('/');
            ToLogDateTime = SwissArmy.DateConvert(this.TextBox_DateTo.Text, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedDateFormat).ToString("yyyyMMdd");
        }

        string PersonnelName = string.Empty;
        if (this.TextBox_User.Text.Length > 0 && this.TextBox_User.Text.CompareTo("[Personnel Name]") != 0)
        {
            PersonnelName = this.TextBox_User.Text;
        } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        LogsCollection ErrorCollection = this.CurLogManager.GetAllGeneralLog(FromLogDateTime, ToLogDateTime, PersonnelName, this.GetLogTypeValue(ddlFiler.SelectedValue)); // Thang Nguyen Edited 07/03/2013

        if (ErrorCollection != null)
        {
            this.ResultTable = new DataTable();

            ResultTable.Columns.Add(new DataColumn("LogDateTime", Type.GetType("System.DateTime")));
            ResultTable.Columns.Add(new DataColumn("LogDateTimeInString", Type.GetType("System.String")));
            ResultTable.Columns.Add(new DataColumn("Claimant", Type.GetType("System.String")));
            //ResultTable.Columns.Add(new DataColumn("ClaimantName", Type.GetType("System.String")));
            ResultTable.Columns.Add(new DataColumn("Description", Type.GetType("System.String")));
            
            //FRANK added jan-2013
            ResultTable.Columns.Add(new DataColumn("log_interface", Type.GetType("System.String")));
            ResultTable.Columns.Add(new DataColumn("UserID", Type.GetType("System.String")));
            ResultTable.Columns.Add(new DataColumn("UserName", Type.GetType("System.String")));
            ResultTable.Columns.Add(new DataColumn("ClaimantID", Type.GetType("System.String")));
            ResultTable.Columns.Add(new DataColumn("ClaimantName", Type.GetType("System.String")));
            ResultTable.Columns.Add(new DataColumn("RoleID", Type.GetType("System.String")));
            ResultTable.Columns.Add(new DataColumn("Action", Type.GetType("System.String")));
            ResultTable.Columns.Add(new DataColumn("ApproverID", Type.GetType("System.String")));
            ResultTable.Columns.Add(new DataColumn("ApproverName", Type.GetType("System.String")));
            ResultTable.Columns.Add(new DataColumn("DfltCurrency", Type.GetType("System.String")));
            ResultTable.Columns.Add(new DataColumn("CreditCardNo", Type.GetType("System.String")));
            ResultTable.Columns.Add(new DataColumn("AprStopPoint", Type.GetType("System.String")));
            ResultTable.Columns.Add(new DataColumn("AprLimit", Type.GetType("System.String")));
            ResultTable.Columns.Add(new DataColumn("NoAprRq", Type.GetType("System.String")));
            ResultTable.Columns.Add(new DataColumn("NoAuthorizerRq", Type.GetType("System.String")));
            ResultTable.Columns.Add(new DataColumn("AprRoute", Type.GetType("System.String")));
            ResultTable.Columns.Add(new DataColumn("AsClaimant", Type.GetType("System.String")));
            ResultTable.Columns.Add(new DataColumn("AsReviewer", Type.GetType("System.String")));
            ResultTable.Columns.Add(new DataColumn("LDAPAddress", Type.GetType("System.String")));
            ResultTable.Columns.Add(new DataColumn("AprRangeFr", Type.GetType("System.Double")));
            ResultTable.Columns.Add(new DataColumn("AprRangeTo", Type.GetType("System.Double")));
            ResultTable.Columns.Add(new DataColumn("LogType", Type.GetType("System.String")));
            ResultTable.Columns.Add(new DataColumn("AprLevel", Type.GetType("System.String")));

            ResultTable.Columns.Add(new DataColumn("UserEmployeeID", Type.GetType("System.String")));
            ResultTable.Columns.Add(new DataColumn("UserEntity", Type.GetType("System.String")));
            ResultTable.Columns.Add(new DataColumn("ClaimantEmployeeID", Type.GetType("System.String")));
            ResultTable.Columns.Add(new DataColumn("ClaimantCompany", Type.GetType("System.String")));
            ResultTable.Columns.Add(new DataColumn("ApproverEmployeeID", Type.GetType("System.String")));

            for (int x = 0; x < ErrorCollection.Count; x++)
            {
                GeneralActionLogItem CurLogItem = (GeneralActionLogItem)ErrorCollection[x];
                DataRow ResultRow = this.ResultTable.NewRow();

                ResultRow["LogDateTime"] = CurLogItem.LogDate;
                ResultRow["LogDateTimeInString"] = SwissArmy.DateConvert(CurLogItem.LogDate, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedDateFormat, "/", true);
                ResultRow["Claimant"] = (CurLogItem.LoggedBy.InternalID.Length == 36) ? CurLogItem.LoggedBy.DisplayName + " (" + CurLogItem.LoggedBy.EmailID + ")" : "";
               // ResultRow["ClaimantName"] = (CurLogItem.LoggedBy.InternalID.Length == 36) ? CurLogItem.LoggedBy.DisplayName : "";
                ResultRow["Description"] = CurLogItem.Description;

                //FRANK add jan-2013
                ResultRow["log_interface"] = CurLogItem.Interface.ToString();
                ResultRow["UserID"] = CurLogItem.UserID;
                ResultRow["UserName"] = CurLogItem.UserName;
                ResultRow["ClaimantID"] = CurLogItem.ClaimantID;
                ResultRow["ClaimantName"] = CurLogItem.ClaimantName;
                ResultRow["RoleID"] = CurLogItem.RoleID;
                ResultRow["Action"] = CurLogItem.Action;
                ResultRow["ApproverID"] = CurLogItem.ApproverID;
                ResultRow["ApproverName"] = CurLogItem.ApproverName;
                ResultRow["DfltCurrency"] = CurLogItem.DfltCurrency;
                ResultRow["CreditCardNo"] = CurLogItem.CreditCardNo;
                ResultRow["AprStopPoint"] = CurLogItem.AprStopPoint;
                ResultRow["AprLimit"] = CurLogItem.AprLimit;
                ResultRow["NoAprRq"] = CurLogItem.NoAprRq;
                ResultRow["NoAuthorizerRq"] = CurLogItem.NoAuthorizerRq;
                ResultRow["AprRoute"] = CurLogItem.AprRoute;
                ResultRow["AsClaimant"] = CurLogItem.AsClaimant;
                ResultRow["AsReviewer"] = CurLogItem.AsReviewer;
                ResultRow["LDAPAddress"] = CurLogItem.LDAPAddress;
                ResultRow["AprRangeFr"] = CurLogItem.AprRangeFr;
                ResultRow["AprRangeTo"] = CurLogItem.AprRangeTo;
                ResultRow["LogType"] = CurLogItem.LogType;
                ResultRow["AprLevel"] = CurLogItem.AprLevel;

                ResultRow["UserEmployeeID"] = CurLogItem.UserEmployeeID;
                ResultRow["UserEntity"] = CurLogItem.UserEntity;
                ResultRow["ClaimantEmployeeID"] = CurLogItem.ClaimantEmployeeID;
                ResultRow["ClaimantCompany"] = CurLogItem.ClaimantCompany;
                ResultRow["ApproverEmployeeID"] = CurLogItem.ApproverEmployeeID;

                this.ResultTable.Rows.Add(ResultRow);
            }
        }
    }

    private void BindData(DataView result)
    {
        this.GridViewResult.DataSource = result;
        this.GridViewResult.DataBind();
    }

    private void BindData(DataView result, int pageindex)
    {
        this.GridViewResult.PageIndex = pageindex;
        this.GridViewResult.DataSource = result;
        this.GridViewResult.DataBind();
    }

    #endregion

    private DataTable datatable4Exp()
    {
        DataTable dtRecords = new DataTable();
        foreach (DataControlField col in GridViewResult.Columns)
            dtRecords.Columns.Add(new DataColumn(col.HeaderText));

        foreach (GridViewRow row in GridViewResult.Rows)
        {
            DataRow dr = dtRecords.NewRow();
            foreach (DataControlFieldCell cell in row.Cells)
            {
                dr[row.Cells.GetCellIndex(cell)] = cell.Text.ToString().Replace("&nbsp;","");
            }
            dtRecords.Rows.Add(dr);
        }
        DataTable dt1 = dtRecords.Copy();

        foreach (DataControlField col in GridViewResult.Columns)
        {
            if (col.Visible == false && dt1.Columns.Contains(col.HeaderText))
                dt1.Columns.Remove (col.HeaderText);
        }

        return dt1;
    }

    protected DataTable BuildDataTableExport(string asLogType)
    {
        this.LoadData();
        DataTable dt = ((DataTable)ViewState["ResultTable"]).Copy();
        Hashtable columns = new Hashtable();

        switch (asLogType)
        {
            case "FullLog":
                //columns.Add("LogDateTime", "Date/Time");
                columns.Add("LogDateTimeInString", "Date/Time");
                columns.Add("Claimant", "Logged By");
                columns.Add("Description", "Description");
                columns.Add("log_interface", "Log Interface");
                columns.Add("UserID", "User ID");
                columns.Add("UserName", "Display User Name");
                columns.Add("ClaimantID", "Claimant ID");
                columns.Add("ClaimantName", "Claimant Name");
                columns.Add("RoleID", "Role");
                columns.Add("Action", "Action");
                columns.Add("ApproverID", "Approver ID");
                columns.Add("ApproverName", "Display Approver Name");
                columns.Add("DfltCurrency", "Default Currency");
                columns.Add("CreditCardNo", "Credit Card No");
                columns.Add("AprStopPoint", "Approval Stop Point");
                columns.Add("AprLimit", "Approving Limit");
                columns.Add("NoAprRq", "No Approval Require");
                columns.Add("NoAuthorizerRq", "No Authorizer Require");
                columns.Add("AprRoute", "Approval Route");
                columns.Add("AsClaimant", "Assistant Claimant");
                columns.Add("AsReviewer", "Assistant Reviewer");
                columns.Add("LDAPAddress", "LDAP Address");
                columns.Add("AprRangeFr", "Approval Range From");
                columns.Add("AprRangeTo", "Approval Range To");
                columns.Add("LogType", "Log Type");
                columns.Add("AprLevel", "Level");
                columns.Add("UserEmployeeID", "User Employee ID");
                columns.Add("UserEntity", "User Entity");
                columns.Add("ClaimantEmployeeID", "Claimant Employee ID");
                columns.Add("ClaimantCompany", "Claimant Company");
                columns.Add("ApproverEmployeeID", "Approver Employee ID");
                break;

            case "Exep":
                //columns.Add("LogDateTime", "Date/Time");
                columns.Add("LogDateTimeInString", "Date/Time");
                columns.Add("Claimant", "Logged By");
                columns.Add("log_interface", "Log Interface");
                columns.Add("UserID", "User ID");
                columns.Add("UserName", "Display User Name");
                columns.Add("Action", "Action");
                columns.Add("AprLimit", "Approving Limit");
                columns.Add("AprLevel", "Level");
                columns.Add("UserEmployeeID", "User Employee ID");
                columns.Add("UserEntity", "User Entity");
                columns.Add("ClaimantEmployeeID", "Claimant Employee ID");
                columns.Add("ClaimantCompany", "Claimant Company");
                columns.Add("ApproverEmployeeID", "Approver Employee ID");
                break;

            case "ClaimantDetail":
                //columns.Add("LogDateTime", "Date/Time");
                columns.Add("LogDateTimeInString", "Date/Time");
                columns.Add("log_interface", "Log Interface");
                columns.Add("ClaimantID", "Claimant ID");
                columns.Add("ClaimantName", "Claimant Name");
                columns.Add("Action", "Action");
                columns.Add("DfltCurrency", "Default Currency");
                columns.Add("CreditCardNo", "Credit Card No");
                columns.Add("AprStopPoint", "Approval Stop Point");
                columns.Add("AprLimit", "Approving Limit");
                columns.Add("NoAprRq", "No Approval Require");
                columns.Add("NoAuthorizerRq", "No Authorizer Require");
                columns.Add("AprRoute", "Approval Route");
                columns.Add("AsClaimant", "Assistant Claimant");
                columns.Add("AsReviewer", "Assistant Reviewer");
                columns.Add("LDAPAddress", "LDAP Address");
                columns.Add("UserEmployeeID", "User Employee ID");
                columns.Add("UserEntity", "User Entity");
                columns.Add("ClaimantEmployeeID", "Claimant Employee ID");
                columns.Add("ClaimantCompany", "Claimant Company");
                columns.Add("ApproverEmployeeID", "Approver Employee ID");
                break;

            case "Authorizer":
                //columns.Add("LogDateTime", "Date/Time");
                columns.Add("LogDateTimeInString", "Date/Time");
                columns.Add("Claimant", "Logged By");
                columns.Add("log_interface", "Log Interface");
                columns.Add("UserID", "User ID");
                columns.Add("UserName", "Display User Name");
                columns.Add("RoleID", "Role");
                columns.Add("Action", "Action");
                columns.Add("AprRangeFr", "Approval Range From");
                columns.Add("AprRangeTo", "Approval Range To");
                columns.Add("UserEmployeeID", "User Employee ID");
                columns.Add("UserEntity", "User Entity");
                columns.Add("ClaimantEmployeeID", "Claimant Employee ID");
                columns.Add("ClaimantCompany", "Claimant Company");
                columns.Add("ApproverEmployeeID", "Approver Employee ID");
                break;
        }
        
        for (int i = dt.Columns.Count-1; i >= 0; i--)
        {
            if (columns.ContainsKey(dt.Columns[i].ColumnName))
            {
                dt.Columns[i].ColumnName = columns[dt.Columns[i].ColumnName].ToString();
            }
            else
                dt.Columns.Remove(dt.Columns[i]);
        }
        return dt;
    }

    protected void btnExport_Click(object sender, EventArgs e)
    {
        try
        {
            DataTable dt = BuildDataTableExport(ddlFiler.SelectedValue);

            if (dt != null)
            {
                Response.AddHeader("Content-Disposition", "attachment;filename=AuditLog.csv");
                Response.ContentType = "text/csv";

                int iColCount = dt.Columns.Count;
                for (int i = 0; i < iColCount - 1; i++)
                {
                    Response.Write("\"" + dt.Columns[i] + "\"");
                    if (i < iColCount - 2)
                    {
                        Response.Write(",");
                    }
                }

                Response.Write(Environment.NewLine);
                // Now write all the rows.

                //foreach (DataRow dr in dt.Rows)
                for (int x = 0; x < dt.Rows.Count; x++)
                {
                    for (int i = 0; i < iColCount - 1; i++)
                    {
                        if (!Convert.IsDBNull(dt.Rows[x][i]))
                        {
                            Response.Write("\"" + dt.Rows[x][i].ToString() + "\"");
                        }

                        if (i < iColCount - 2)
                        {
                            Response.Write(",");
                        }

                    }
                    Response.Write(Environment.NewLine);
                }
                Response.End();
            }
        }
        catch (Exception ex)
        {
            //ShowMessage(ex.ToString);
        }
    }

    protected void ddlFiler_SelectedIndexChanged(object sender, EventArgs e)
    {
        //string str = ddlFiler.SelectedValue.ToString();
        //GridBuild(str);
    }

    protected void GridBuild(string str)
    {
        //string FilterStr = "";
        switch (str)
        {
            case "FullLog":
                {
                    //FilterStr = "";

                    for (int i = 1; i <= 25; i++)
                    {
                        GridViewResult.Columns[i].Visible = true;
                    }

                    //for (int i = 1; i <= 24; i++)
                    //{
                    //    GridViewResult.Columns[i].Visible = false;
                    //}
                    //GridViewResult.Columns[2].Visible = true;//description
                    //GridViewResult.Columns[3].Visible = true; //log inteface
                    break;
                }
            case "Exep":
                {
                    //FilterStr = "'GonzalesAdminEASDetail'";
                    for (int i = 1; i <= 25; i++)
                    {
                        GridViewResult.Columns[i].Visible = false;
                    }
                    GridViewResult.Columns[1].Visible = true; //log-by
                    GridViewResult.Columns[3].Visible = true; //log inteface
                    GridViewResult.Columns[4].Visible = true; //userid
                    GridViewResult.Columns[5].Visible = true; //username

                    GridViewResult.Columns[9].Visible = true; //action
                    GridViewResult.Columns[10].Visible = true; //approverID
                    GridViewResult.Columns[11].Visible = true; //approverName
                    GridViewResult.Columns[15].Visible = true; //approvingLimit // Thang nguyen added 20/03/2013
                    // Thang Nguyen comments: 20/03/2013 -->refer to e-mail requirement
                    //GridViewResult.Columns[22].Visible = true; //approval from 
                    //GridViewResult.Columns[23].Visible = true; //approval to
                    GridViewResult.Columns[25].Visible = true; //Level
                    break;
                }
            case "ClaimantDetail":
                {
                    //FilterStr = "'GonzalesAdminClaimantsDetail'";
                    for (int i = 1; i <= 25; i++)
                    {
                        GridViewResult.Columns[i].Visible = false;
                    }

                    GridViewResult.Columns[0].Visible = true; //Date/Time
                    GridViewResult.Columns[1].Visible = true; //Logged By
                    GridViewResult.Columns[3].Visible = true; //log_interface
                    GridViewResult.Columns[6].Visible = true; //ClaimantID
                    GridViewResult.Columns[7].Visible = true; //ClaimantName
                    GridViewResult.Columns[9].Visible = true; //Action
                    GridViewResult.Columns[12].Visible = true; //DfltCurrency
                    GridViewResult.Columns[13].Visible = true; //CreditCardNo
                    GridViewResult.Columns[14].Visible = true; //AprStopPoint
                    GridViewResult.Columns[15].Visible = true; //AprLimit
                    GridViewResult.Columns[16].Visible = true; //NoAprRq
                    GridViewResult.Columns[17].Visible = true; //NoAuthorizerRq
                    GridViewResult.Columns[18].Visible = true; //NoAuthorizerRq
                    GridViewResult.Columns[19].Visible = true; //AprRoute
                    GridViewResult.Columns[20].Visible = true; //AsClaimant
                    GridViewResult.Columns[21].Visible = true; //AsReviewer

                    break;
                }
            case "Authorizer":
                {
                    //FilterStr = "'GonzalesAdminFIListing','GonzalesAdminAuthorizedListing','GonzalesAdminAuthorizedDetail'";
                    for (int i = 1; i <= 25; i++)
                    {
                        GridViewResult.Columns[i].Visible = false;
                    }
                    GridViewResult.Columns[1].Visible = true; //log-by
                    GridViewResult.Columns[3].Visible = true; //log inteface
                    GridViewResult.Columns[4].Visible = true; //userid
                    GridViewResult.Columns[5].Visible = true; //username

                    GridViewResult.Columns[8].Visible = true; //Roles
                    GridViewResult.Columns[9].Visible = true; //action

                    GridViewResult.Columns[8].Visible = true; //RoleID
                    GridViewResult.Columns[9].Visible = true; //Action

                    GridViewResult.Columns[22].Visible = true; //approval from
                    GridViewResult.Columns[23].Visible = true; //approval to
                    break;
                }
        }
        //Filter by log type
        DataView ResultView = new DataView(this.ResultTable);
        //if (this.ResultTable != null && FilterStr != "")
        //{
        //    ResultView.RowFilter = "LogType in (" + FilterStr + ")";
        //}
        this.CurrentPageIndex = 0;
        this.BindData(ResultView, this.CurrentPageIndex);
        if (ResultView.Count == 0)
            this.ShowMessage("There's no data.");
    }
}
